package com.kangaroo.myhome.utils;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @ClassName ExcelExport2
 * @Description TODO
 * @Author kangaroo
 * @Date 2020/8/2 22:02
 * @Version 1.0
 **/
public class ExcelExport2 {
    /**
     * 将传入的数据导出excel表并下载
     * 
     * @param response 返回的HttpServletResponse
     * @param importlist 要导出的对象的集合
     * @param attributeNames 含有每个对象属性在excel表中对应的标题字符串的数组（请按对象中属性排序调整字符串在数组中的位置）
     */
    public static void export(HttpServletResponse response, List<?> importlist, String[] attributeNames) {
        System.out.println("export ========================");
        // 获取数据集
        List<?> datalist = importlist;

        // 声明一个工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 生成一个表格
        HSSFSheet sheet = workbook.createSheet();
        // 设置表格默认列宽度为15个字节
        sheet.setDefaultColumnWidth((short)18);

        // 获取字段名数组
        String[] tableAttributeName = attributeNames;
        // 获取对象属性
        Field[] fields = ClassUtil.getClassAttribute(importlist.get(0));
        // 获取对象get方法
        List<Method> methodList = ClassUtil.getMethodGet(importlist.get(0));

        // 循环字段名数组，创建标题行
        Row row = sheet.createRow(0);
        for (int j = 0; j < tableAttributeName.length; j++) {
            // 创建列
            Cell cell = row.createCell(j);
            // 设置单元类型为String
            cell.setCellType(CellType.STRING);
            cell.setCellValue(transCellType(tableAttributeName[j]));
        }
        // 创建普通行
        for (int i = 0; i < datalist.size(); i++) {
            // 因为第一行已经用于创建标题行，故从第二行开始创建
            row = sheet.createRow(i + 1);
            // 如果是第一行就让其为标题行
            Object targetObj = datalist.get(i);
            for (int j = 0; j < fields.length; j++) {
                // 创建列
                Cell cell = row.createCell(j);
                cell.setCellType(CellType.STRING);
                //
                try {
                    Object value = methodList.get(j).invoke(targetObj, new Object[] {});
                    cell.setCellValue(transCellType(value));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                    System.out.println("IllegalAccessException ========================" + e.getMessage());
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                    System.out.println("InvocationTargetException ========================" + e.getMessage());
                }
            }
        }



        /*String folderPath = ResourceBundle.getBundle("systemconfig").getString("downloadFolder") + File.separator + "inspFile";

        //创建上传文件目录
        File folder = new File(folderPath);
        //如果文件夹不存在创建对应的文件夹
        if (!folder.exists()) {
            folder.mkdirs();
        }
        SimpleDateFormat format = new SimpleDateFormat("YYYYMMddHHmm");
        //设置文件名
        String fileName = "项目信息清单" + format.format(new Date())  + ".xls";
        String savePath = folderPath + File.separator + fileName;
        // System.out.println(savePath);

        try {
            OutputStream fileOut = new FileOutputStream(savePath);
            workbook.write(fileOut);
            fileOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "inspFile"+ File.separator +fileName;*/

        System.out.println("export ========================1234");
        response.setCharacterEncoding("UTF-8");
        SimpleDateFormat format = new SimpleDateFormat("YYYYMMddHHmm");
        //设置文件名
        String fileName = "项目信息清单" + format.format(new Date())  + ".xls";
        response.setContentType("application/octet-stream");
        // response.setContentType("application/vnd.ms-excel");

        // 默认Excel名称
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName +".xls");

        try {
            response.flushBuffer();
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public static String exportPath(HttpServletResponse response, List<?> importlist, String[] attributeNames) {
        // 获取数据集
        List<?> datalist = importlist;

        // 声明一个工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 生成一个表格
        HSSFSheet sheet = workbook.createSheet();
        // 设置表格默认列宽度为15个字节
        sheet.setDefaultColumnWidth((short)18);

        // 获取字段名数组
        String[] tableAttributeName = attributeNames;
        // 获取对象属性
        Field[] fields = ClassUtil.getClassAttribute(importlist.get(0));
        // 获取对象get方法
        List<Method> methodList = ClassUtil.getMethodGet(importlist.get(0));

        // 循环字段名数组，创建标题行
        Row row = sheet.createRow(0);
        for (int j = 0; j < tableAttributeName.length; j++) {
            // 创建列
            Cell cell = row.createCell(j);
            // 设置单元类型为String
            cell.setCellType(CellType.STRING);
            cell.setCellValue(transCellType(tableAttributeName[j]));
        }
        // 创建普通行
        for (int i = 0; i < datalist.size(); i++) {
            // 因为第一行已经用于创建标题行，故从第二行开始创建
            row = sheet.createRow(i + 1);
            // 如果是第一行就让其为标题行
            Object targetObj = datalist.get(i);
            for (int j = 0; j < fields.length; j++) {
                // 创建列
                Cell cell = row.createCell(j);
                cell.setCellType(CellType.STRING);
                //
                try {
                    Object value = methodList.get(j).invoke(targetObj, new Object[] {});
                    cell.setCellValue(transCellType(value));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }



        String folderPath = ResourceBundle.getBundle("systemconfig").getString("downloadFolder") + File.separator + "inspFile";

        //创建上传文件目录
        File folder = new File(folderPath);
        //如果文件夹不存在创建对应的文件夹
        if (!folder.exists()) {
            folder.mkdirs();
        }
        SimpleDateFormat format = new SimpleDateFormat("YYYYMMddHHmm");
        //设置文件名
        String fileName = "项目信息清单" + format.format(new Date())  + ".xls";
        String savePath = folderPath + File.separator + fileName;
        // System.out.println(savePath);

        try {
            OutputStream fileOut = new FileOutputStream(savePath);
            workbook.write(fileOut);
            fileOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "inspFile"+ File.separator +fileName;



        // response.setContentType("application/octet-stream");
        /*response.setContentType("application/vnd.ms-excel");

        // 默认Excel名称
        response.setHeader("Content-Disposition", "attachment;fileName=" + "test.xls");

        try {
            response.flushBuffer();
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }*/

    }

    private static String transCellType(Object value) {
        String str = null;
        if (value instanceof Date) {
            Date date = (Date)value;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            str = sdf.format(date);
        } else {
            str = String.valueOf(value);
            if (str == "null") {
                str = "";
            }
        }

        return str;
    }

}
